package com.example.chapter04;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Date;
import java.util.Objects;

public class ActionReceiveActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tv_receive;
    TextView tv_response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_receive);
        tv_receive = findViewById(R.id.tv_receive);

        findViewById(R.id.btn_response).setOnClickListener(this);
        tv_response = findViewById(R.id.tv_response);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String msg = extras.getString("send");
            if (msg == null) msg = "";
            tv_receive.setText(Objects.requireNonNull(extras.getString("time")).concat("\n" + msg));
        }

    }

    @Override
    public void onClick(View view) {
        Intent backIntent = new Intent();
        Bundle backBundle = new Bundle();
        backBundle.putString("time", new Date().toString());
        backBundle.putString("response", tv_response.getText().toString());
        Log.d("+++++++", backBundle.getString("time"));
        backIntent.putExtras(backBundle);
        // 携带意图返回上一个页面，RESULT_OK表示处理成功
        setResult(Activity.RESULT_OK, backIntent);
        finish();
    }
}